package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.pojo.FoodUser;
import com.cqut.hfh.ordering.service.IFodUserService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/25-23:15
 */
@RequestMapping("/loginandregist")
@Controller
public class FodUserController {

    @Resource
    private IFodUserService fodUserService;
    /**
     * 会员登录
     * @param user_name
     * @param user_pwd
     * @return
     */
    @RequestMapping("/loginSubmit")
    public String loginSubmit(@RequestParam("userName")String user_name,
                              @RequestParam("userPwd")String user_pwd,
                              Model model, HttpSession session){

        FoodUser temp = fodUserService.login(user_name, user_pwd);
        //失败
        if( temp == null){
            model.addAttribute("mes", "用户名或密码错误");
            return "yyf/login";
        }
        //成功
        session.setAttribute("fodUser", temp.getUser_name());
        session.setAttribute("userId", temp.getUser_id());

        return "tl/index";
    }

    /**
     * 会员注册
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/registSubmit")
    public String registSubmit(FoodUser user, Model model){

        int temp = fodUserService.register(user);

        //失败
        if(temp == 0){
            model.addAttribute("mes", "用户失败");
            return "yyf/regist";
        }else if(temp == 1){
            //成功
            model.addAttribute("mes", "注册成功");
            return "yyf/login";
        }else {
            //身份证号
            model.addAttribute("mes", "该身份用户已注册");
            return "yyf/regist";
        }

    }


    /**
     * 获取所有会员
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findAlls", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findUsers(@RequestParam("limit") String limit,
                            @RequestParam("page") String page){

        List<Map<String, Object>> user_list = fodUserService.getFodUsers(limit, page);
        for( Map<String, Object> map : user_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }
        Integer count = fodUserService.getUsersCount();

        return Message.getReturnMsg(0,"", count, user_list);
    }


    /**
     * 通过ID查找会员信息
     * @param user_id
     * @return
     */
    @PostMapping("/findById")
    @ResponseBody
    public FoodUser findById(@RequestParam("id") String user_id){

        Map<String, Object> result_map = fodUserService.findUserById(user_id);
        FoodUser e = new FoodUser();
        e.setProperties(result_map);

        return e;
    }

    /**
     * 根据名称查询
     * @param limit
     * @param page
     * @param user_name
     * @return
     */
    @RequestMapping(path = "/findUsersByName", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findRolesByName(@RequestParam("limit") String limit,
                                  @RequestParam("page") String page,
                                  @RequestParam("user_name") String user_name){

        List<Map<String, Object>> user_list = fodUserService.getUsersByName(limit, page, user_name);

        for( Map<String, Object> map : user_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = user_list.size();

        return Message.getReturnMsg(0,"", count, user_list);
    }


    @RequestMapping(path = "/userDelete", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String deleteById(@RequestParam("id") String user_id){

        int temp = fodUserService.delById(user_id);
        if( temp == 2){
            return "{\"msg\":\"ok\"}";
//            return Message.getReturnMsg(0,"ok");
        }
        return "{\"msg\":\"fail\"}";
//        return MessageUtil.getReturnMsg(0,"fail",null);
    }



    @RequestMapping(path = "/saveOrUpdateUser", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String saveOrUpdate(FoodUser user){

        user.setOptime(new Date());

        if( user.getUser_id() == null){
            user.setUser_id(EntityIdutils.getId());
            int temp = fodUserService.saveUser(user);
            return Message.getReturnMsg(0,"");
        }else{
            int temp = fodUserService.updateUser(user);
        }
        return "";
    }

}
